/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystem;

import api.Rights.RightsEnum;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Tomas
 */
@XmlRootElement
public class FileNode implements Cloneable{

    private FileStatusEnum status;
    private Date lastChange;
    private List<FileNode> childs;
    private File file;
    private int version;
    private String oldName;
    private boolean directory;
    private boolean waitingForUpdate = false;
    private RightsEnum rights;
    
    public FileNode() {
    }

    public FileNode(File file, FileStatusEnum status, Date lastChange, List<FileNode> childs) {
        this();
        this.file = file;
        this.directory = file.isDirectory();
        this.status = status;
        this.lastChange = lastChange;
        this.childs = childs;
    }
        
    @XmlAttribute
    public String getName() {
        return file.getName();
    }

    public List<FileNode> getChilds() {
        if (childs == null) {
            childs = new ArrayList<>();
        }
        FileNodeUtil.sort(childs);
        return childs;
    }

    public void setChilds(List<FileNode> childs) {
        this.childs = childs;
    }

    public Long getLastChange() {
        return lastChange.getTime();
    }

    public void setLastChange(Long lastChange) {
        this.lastChange = new Date(lastChange);
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
        this.directory = file.isDirectory();
    }

    public FileStatusEnum getStatus() {
        return status;
    }

    public void setStatus(FileStatusEnum status) {
        this.status = status;
    }

    public String getOldName() {
        return oldName;
    }

    public void setOldName(String oldName) {
        this.oldName = oldName;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @Override
    public String toString() {
        return file.getName();
    }

    public boolean isLeaf() {
       return !directory;
    }

    public FileNode doClone(){
        FileNode node = null;
        try {
             node = (FileNode)super.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(FileNode.class.getName()).log(Level.SEVERE, null, ex);
        }
        return node;
    }

    public boolean isWaitingForUpdate() {
        return waitingForUpdate;
    }

    public void setWaitingForUpdate(boolean waitingForUpdate) {
        this.waitingForUpdate = waitingForUpdate;
    }

    public RightsEnum getRights() {
        return rights;
    }

    public void setRights(RightsEnum rights) {
        this.rights = rights;
    }
    
    
}
